package com.springboot.prac.rabbitmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Desc:
 * @Author: siyuanlong
 * @Date: 2022/2/22 22:19
 */
public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.139.131");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        // 2.创建一个Connection
        Connection connection = connectionFactory.newConnection();

        // 3.获取一个信道
        Channel channel = connection.createChannel();

        channel.exchangeDeclare("cc", BuiltinExchangeType.DIRECT, true);

        // 4.通过信道发送消息
        for (int i = 0; i < 100; i++) {
            channel.basicPublish("cc", "msg01", null, "haha".getBytes());
        }

        // 5.关闭资源
        channel.close();
        connection.close();
    }
}
